package com.qq.BFMRSE.BF_MRSEController;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.qq.BFMRSE.BussinessService.userBusSerImpl;
import com.qq.BFMRSE.model.user;

@Controller
public class handlerRegister {
	@RequestMapping(value= "/register",method=RequestMethod.POST)
	public void handler(HttpServletRequest request,HttpServletResponse response){
		 response.setContentType("text/html;charset=utf-8");
	     response.setCharacterEncoding("utf-8");
	 	WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		userBusSerImpl ubIm=(userBusSerImpl) wac.getBean("usBuIm");
		Logger log=Logger.getLogger(handlerRegister.class);
	     String userName=request.getParameter("userName");
	     String pwd=request.getParameter("pwd");
	     String email=request.getParameter("email");
	     user newUser=new user(userName, email, pwd);
	     boolean flag=ubIm.isUserNameUnUsed(userName);
	     if(flag){
	    	 ubIm.addNewUser(newUser);
	    	 log.info("new user:"+userName+"register.");	    	 
	     }
	     PrintWriter out=null;
		 try {  
		        out = response.getWriter();
		        out.println(flag);
		          
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    } finally {  
		        if (out != null) {  
		        	out.flush();
		            out.close();  
		        }  
		    }  
	}
}
